<template>
  <el-dialog
    :title="!dataForm.machId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备地址" prop="machAdd">
      <el-input v-model="dataForm.machAdd" placeholder="机器地址"></el-input>
    </el-form-item>
<!--    <el-form-item label="机器状态" prop="machStatus">-->
<!--&lt;!&ndash;      (0:未启用，1:正常，2:警告，3:无信号)&ndash;&gt;-->
<!--      <el-radio-group v-model="dataForm.machStatus">-->
<!--        <el-radio :label="0">未启用</el-radio>-->
<!--        <el-radio :label="1">正常</el-radio>-->
<!--        <el-radio :label="2">异常</el-radio>-->
<!--      </el-radio-group>-->
<!--    </el-form-item>-->
    <el-form-item label="负责人 id" prop="chargeId">
      <el-input v-model="dataForm.chargeId" placeholder="负责人id"></el-input>
    </el-form-item>
    <el-form-item label="负责人2" prop="charge2Id">
      <el-input v-model="dataForm.charge2Id" placeholder="负责人2的id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          machId: 0,
          machAdd: '',
          machStatus: '',
          chargeId: '',
          charge2Id: '',
          isDeleted: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          machAdd: [
            { required: true, message: '机器地址不能为空', trigger: 'blur' }
          ],

          chargeId: [
            { required: true, message: '负责人id不能为空', trigger: 'blur' }
          ],
          charge2Id: [
            { required: true, message: '负责人2的id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.machId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.machId) {
            this.$http({
              url: this.$http.adornUrl(`/alarm/machine/info/${this.dataForm.machId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.machAdd = data.machine.machAdd
                this.dataForm.chargeId = data.machine.chargeId
                this.dataForm.charge2Id = data.machine.charge2Id
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/alarm/machine/${!this.dataForm.machId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'machId': this.dataForm.machId || undefined,
                'machAdd': this.dataForm.machAdd,
                'machStatus': this.dataForm.machStatus,
                'chargeId': this.dataForm.chargeId,
                'charge2Id': this.dataForm.charge2Id,
                'isDeleted': this.dataForm.isDeleted,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
